# coding=utf-8
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from fe_api.assistance.utils import view_funcs


class Info(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, user_transcription_id):
        info = view_funcs.get_info(user_transcription_id)
        if not info:
            return Response({
                "status": 404,
                "msg": "not found",
            })

        return Response({
            "status": 200,
            "msg": "success",
            "data": info,
        })


class CreateAssistance(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAuthenticated,)

    def post(self, request, user_transcription_id):
        """
        进入抄写
        """
        page_num = view_funcs.entry_assistance(
            request.user, user_transcription_id)
        if page_num == -1:
            return Response({
                "status": 410,
                "msg": "gone",
            })

        return Response({
            "status": 200,
            "msg": "success",
            "data": {
                "page_num": page_num,
            }
        })


class Details(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, user_transcription_id):
        """
        获取助抄详情
        """
        details = view_funcs.get_assistance_details(user_transcription_id)
        if not details:
            return Response({
                "status": 404,
                "msg": "not found",
            })

        return Response({
            "status": 200,
            "msg": "success",
            "data": details,
        })
